package model
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class VirtualPetView extends Sprite
	{
		private static const numGraphicsToLoad:int = 4;
		
		private var pet:VirtualPet;
		private var graphicsContainer:Sprite;
		
		private var petAlive:Loader;
		private var petDead:Loader;
		private var foodHungry:Loader;
		private var foodStarving:Loader;
		
		private var numGraphicsLoaded:int;
		private var petName:TextField;
		
		private var appleBtn:FoodButton;
		private var sushiBtn:FoodButton;
		
		public function VirtualPetView(pet:VirtualPet)
		{
			this.pet = pet;
			pet.addEventListener(VirtualPet.STATE_CHANGE, stateChangeListener);
			pet.addEventListener(VirtualPet.NAME_CHANGE, nameChangeListener);
			
			createGraphicsContainer();
			createNameTag();
			loadGraphics();
			addButtons();
			
			renderCurrentPetName();
		}
		
		private function addButtons():void
		{
			appleBtn = new FoodButton("Feed apple");
			appleBtn.x = 20;
			appleBtn.y = 200;
			appleBtn.addEventListener(MouseEvent.CLICK, appleButtonListenr);
			
			sushiBtn = new FoodButton("Feed suschi");
			sushiBtn.x = 20;
			sushiBtn.y = 230;
			sushiBtn.addEventListener(MouseEvent.CLICK, sushiButtonListener);
			
			addChild(appleBtn);
			addChild(sushiBtn);
		}
		
		protected function sushiButtonListener(event:MouseEvent):void
		{
			pet.eat(new Sushi);
		}
		
		protected function appleButtonListenr(event:MouseEvent):void
		{
			pet.eat(new Apple);
		}
		
		private function createGraphicsContainer():void
		{
			graphicsContainer = new Sprite();
			addChild(graphicsContainer);
		}
		
		private function createNameTag():void
		{
			petName = new TextField();
			petName.defaultTextFormat = new TextFormat("_sans", 14, 0x006666, true);
			petName.autoSize = TextFieldAutoSize.CENTER;
			petName.selectable = false;
			petName.x = 250;
			petName.y = 20;
			addChild(petName);
		}
		
		private function stateChangeListener(event:Event):void
		{
			renderCurrentPetState();
		}
		private function nameChangeListener(event:Event):void
		{
			renderCurrentPetName();
		}
		
		private function renderCurrentPetName():void
		{
			petName.text = pet.getName();
		}		
		
		private function renderCurrentPetState():void
		{
			while(graphicsContainer.numChildren > 0)
			{
				graphicsContainer.removeChildAt(0);
			}
			var state:int = pet.getPetState();
			
			switch(state)
			{
				case VirtualPet.PETSTATE_DEAD:
					graphicsContainer.addChild(petDead);
					break;
				case VirtualPet.PETSTATE_STARVING:
					graphicsContainer.addChild(petAlive);
					graphicsContainer.addChild(foodStarving);
					break;
				case VirtualPet.PETSTATE_HUNGRY:
					graphicsContainer.addChild(petAlive);
					graphicsContainer.addChild(foodHungry);
					break;
				case VirtualPet.PETSTATE_FULL:
					graphicsContainer.addChild(petAlive);
					break;
			}
		}
		
		private function loadGraphics():void
		{
			petAlive = new Loader();
			petAlive.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
			petAlive.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
			petAlive.load(new URLRequest("assets/alive.gif"));
			
			petDead = new Loader();
			petDead.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
			petDead.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
			petDead.load(new URLRequest("assets/died.gif"));
			
			foodHungry = new Loader();
			foodHungry.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
			foodHungry.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
			foodHungry.load(new URLRequest("assets/hungry.gif"));
			foodHungry.x = 15;
			foodHungry.y = 100;
			
			foodStarving = new Loader();
			foodStarving.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
			foodStarving.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
			foodStarving.load(new URLRequest("assets/starving.gif"));
			foodStarving.x = 15;
			foodStarving.y = 100;
		}
		
		protected function ioErrorListener(event:IOErrorEvent):void
		{
			trace("Load error " + event);
		}
		
		protected function completeListener(event:Event):void
		{
			numGraphicsLoaded++;
			if(numGraphicsLoaded == numGraphicsToLoad)
			{
				renderCurrentPetState();
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
	}
}